<template>
  <div class="form-mask">
    <div class="form-wrapper">
      <img class="logo" :src="BASE_URL + configuration.logo" />
      <div class="header" v-html="configuration.title"></div>
      <div class="input-wrapper">
        <div class="border-wrapper">
          <input type="text" v-model="form.username" name="username" class="border-item" placeholder="username" />
        </div>
        <div class="border-wrapper">
          <input type="password" v-model="form.password" name="password" class="border-item" placeholder="password" />
        </div>
        <div class="border-wrapper">
          <input
            @keyup.enter="login"
            type="text"
            v-model="form.VerifyCode"
            name="VerifyCode"
            class="border-item code-input"
            placeholder="code"
          />
          <img @click="getKey" class="border-item code-img" :src="form.imgSrc" alt="" />
        </div>
      </div>
      <div class="action">
        <div class="btn" @click="login" >login</div>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, getCurrentInstance } from 'vue';
import { BASE_URL } from '../../utils/config';
let md5 = require('md5');
export default {
  name: 'Login',
  setup() {
    const { ctx } = getCurrentInstance();
    let form = reactive({
      username: '',
      password: '',
      VerifyCode: '',
      key: '',
      imgSrc: ''
    });

    async function getKey() {
      let key = await ctx.$fetch.post('/Publics/getVerifyKey');
      form.key = key.data.key;
      form.VerifyCode = '';
      form.imgSrc = BASE_URL + '/admin_api.php/Publics/verify?key=' + form.key;
    }
    getKey();

    let configuration = reactive({
      title: '',
      logo: ''
    });
    async function getConfig() {
      await ctx.$store.dispatch('GET_CONFIGURE');
      configuration.title = ctx.$store.state.configuration.title;
      configuration.logo = ctx.$store.state.configuration.login_logo;
    }
    getConfig();

    async function login() {
      let data = { ...form };
      data.password = md5(data.password);
      try {
        let user = await ctx.$fetch.post('/publics/login', data);
        ctx.$message.success(user.msg);
        ctx.$store.dispatch('SET_TOKEN', user.data.token);

        await ctx.$store.dispatch('GET_USERINFO');
        ctx.$router.push({
          name: 'Home'
        });
      } catch (error) {
        getKey();
      }
    }

    return {
      form,
      login,
      configuration,
      getConfig,
      BASE_URL,
      getKey
    };
  }
};
</script>

<style lang="scss" scoped>
.form-mask {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background-color: rgb(41, 45, 62);
}
.form-wrapper {
  width: 300px;
  padding: 30px;
  color: #fff;
  text-align: center;
  background-color: #001529;
  border-radius: 2px;
}

.form-wrapper .header {
  font-size: 25px;
  line-height: 100px;
  text-align: center;
  // text-transform: uppercase;
}

.form-wrapper .logo {
  width: 75px;
  height: 90px;
}

.form-wrapper .input-wrapper input {
  width: 100%;
  font-size: 15px;
  color: #fff;
  text-align: center;
  background-color: rgb(41, 45, 62);
  border: 0;
  outline: none;
}

.form-wrapper .input-wrapper input::placeholder {
  text-transform: uppercase;
}

.form-wrapper .input-wrapper .border-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 50px;
  margin-bottom: 20px;
  background-image: linear-gradient(to right, #e8198b, #0eb4dd);
  border-radius: 30px;
  .code-input {
    flex: 1;
    border-radius: 30px 0 0 30px !important;
  }
  .code-img {
    width: 100px !important;
    cursor: pointer;
    border-radius: 0 30px 30px 0 !important;
  }
}

.form-wrapper .input-wrapper .border-wrapper .border-item {
  width: calc(100% - 4px);
  height: calc(100% - 4px);
  border-radius: 30px;
}

.form-wrapper .action {
  display: flex;
  justify-content: center;
}

.form-wrapper .action .btn {
  width: 60%;
  line-height: 50px;
  text-align: center;
  text-transform: uppercase;
  cursor: pointer;
  border: 2px solid #0e92b3;
  border-radius: 30px;
}

.form-wrapper .action .btn:hover {
  background-color: #0e92b3;
}

.form-wrapper .icon-wrapper {
  width: 60%;
  padding: 20px;
  margin: 0 auto;
  margin-top: 20px;
  text-align: center;
  border-top: 1px dashed rgb(146, 146, 146);
}

.form-wrapper .icon-wrapper i {
  padding: 5px;
  font-size: 20px;
  color: rgb(187, 187, 187);
  cursor: pointer;
  border: 1px solid #fff;
  border-radius: 20px;
}

.code-box {
  display: flex;
}
</style>
